package com.mce.nebula.controller;

import com.mce.nebula.dto.ApiResponse;
import com.mce.nebula.service.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置管理控制器
 * 对应Python的ConfigOSSView, ConfigRUDView, LogoView
 */
@Slf4j
@RestController
@RequestMapping("/config")
@Tag(name = "配置管理", description = "系统配置管理相关接口")
@RequiredArgsConstructor
public class ConfigController {
    
    private final ConfigService configService;
    
    @GetMapping("/oss")
    @Operation(summary = "获取OSS配置", description = "获取对象存储配置信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getOSSConfig() {
        try {
            Map<String, Object> ossConfig = configService.getOSSConfig();
            return ResponseEntity.ok(ApiResponse.success(ossConfig));
        } catch (Exception e) {
            log.error("Failed to get OSS config", e);
            return ResponseEntity.badRequest()
                .body(ApiResponse.error(400, e.getMessage()));
        }
    }
    
    @PutMapping("/oss")
    @Operation(summary = "更新OSS配置", description = "更新对象存储配置信息")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateOSSConfig(@RequestBody Map<String, Object> config) {
        try {
            Map<String, Object> result = configService.updateOSSConfig(config);
            return ResponseEntity.ok(ApiResponse.success(result, "OSS配置更新成功"));
        } catch (Exception e) {
            log.error("Failed to update OSS config", e);
            return ResponseEntity.badRequest()
                .body(ApiResponse.error(400, e.getMessage()));
        }
    }
    
    @GetMapping("/info")
    @Operation(summary = "获取系统配置信息", description = "获取平台配置信息")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getConfigInfo() {
        try {
            Map<String, Object> configInfo = configService.getConfigInfo();
//            Map<String, Object> data = new HashMap<>();
//            data.put("bean", configInfo);
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(true, "query success", "Logo获取成功", 200, new ApiResponse.ResponseBean<>(configInfo,null));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to get config info", e);
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "error", e.getMessage(), 400, null);
            return ResponseEntity.badRequest()
                .body(response);
        }
    }
    
    @PutMapping("/info")
    @Operation(summary = "更新配置信息", description = "更新指定的配置项")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateConfigInfo(
            @RequestParam String key,
            @RequestBody Map<String, Object> request) {
        try {
            if (key == null || key.isEmpty()) {
                ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "no found config key", "更新失败", 404, null);
                return ResponseEntity.status(404).body(response);
            }
            
            Object value = request.get(key);
            if (value == null) {
                ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "no found config value", "更新失败", 404, null);
                return ResponseEntity.status(404).body(response);
            }
            
            Map<String, Object> resultData = configService.updateConfig(key, value);
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(true, "success", "更新成功", 200, new ApiResponse.ResponseBean<>(resultData,null));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to update config: {}", key, e);
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "error", "更新失败", 400, null);
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    @DeleteMapping("/info")
    @Operation(summary = "删除配置信息", description = "删除指定的配置项")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteConfigInfo(
            @RequestParam String key,
            @RequestBody Map<String, Object> request) {
        try {
            if (key == null || key.isEmpty()) {
                ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "no found config key", "重置失败", 404, null);
                return ResponseEntity.status(404).body(response);
            }
            
            Object value = request.get(key);
            if (value == null) {
                ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "no found config value", "重置失败", 404, null);
                return ResponseEntity.status(404).body(response);
            }
            
            Map<String, Object> resultData = configService.deleteConfig(key);
            Map<String, Object> data = new HashMap<>();
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(true, "success", "重置成功", 200, new ApiResponse.ResponseBean<>(resultData,null));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Failed to delete config: {}", key, e);
            ApiResponse<Map<String, Object>> response = new ApiResponse<>(false, "error", "重置失败", 400, null);
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    @GetMapping("/logo")
    @Operation(summary = "获取Logo", description = "获取系统Logo信息")
    public ResponseEntity<ApiResponse<Map<String, String>>> getLogo(HttpServletRequest request) {
        try {
            Map<String, String> logoInfo = configService.getLogo(request);
            return ResponseEntity.ok(ApiResponse.success(logoInfo, "Logo获取成功"));
        } catch (Exception e) {
            log.error("Failed to get logo", e);
            return ResponseEntity.badRequest()
                .body(ApiResponse.error(400, e.getMessage()));
        }
    }
}